import 'package:flutter/foundation.dart';
import 'package:flutterlib/common/constants/constants.dart';
import 'package:flutterlib/common/net/retrofit/user_api.dart';
import 'package:flutterlib/model/interest/interest_bean.dart';
import 'package:geolocator/geolocator.dart';
import 'package:mobx/mobx.dart';
import 'package:thrio/thrio.dart';

import 'common/net/retrofit/interest_api.dart';
import 'common/utils/preferences_service.dart';
import 'common/utils/utils.dart';
import 'model/user/user_entity.dart';
import 'package:flutterlib/common/datacenter/user_center.dart';

import 'pages/interest/viewmodel/interest_store.dart';

part 'settings_store.g.dart';

///保存了需要全局观察的变量，用户信息
class SettingsStore = _SettingsStore with _$SettingsStore;

const String KEY_USER_PHONE_INFO = "KEY_USER_PHONE_INFO";
abstract class _SettingsStore with Store {


  _SettingsStore(this._preferencesService) {
  }

  Future<Position> _determinePosition() async {
    bool serviceEnabled;
    LocationPermission permission;

    serviceEnabled = await Geolocator.isLocationServiceEnabled();
    if (!serviceEnabled) {
      return Future.error('Location services are disabled.');
    }

    permission = await Geolocator.checkPermission();
    if (permission == LocationPermission.deniedForever) {
      return Future.error(
          'Location permissions are permantly denied, we cannot request permissions.');
    }

    if (permission == LocationPermission.denied) {
      permission = await Geolocator.requestPermission();
      if (permission != LocationPermission.whileInUse &&
          permission != LocationPermission.always) {
        return Future.error(
            'Location permissions are denied (actual value: $permission).');
      }
    }

    return await Geolocator.getCurrentPosition();
  }

  final PreferencesService _preferencesService;

  InterestStore interestStore = InterestStore();

  @observable
  Position myPosition;


  @observable
  UserEntity userEntity;

  @computed
  String get token => userEntity?.token??'';

  @computed
  bool get isLogin => userEntity != null;

  @action
  setUserEntity(UserEntity loginUser) async{
    userEntity = loginUser;
    if(loginUser != null) {
      trueDebugPrint("flutterting setUserEntity ${loginUser.toJson()}");
      if (interests.isEmpty) {
        trueDebugPrint("flutterting get my interests");
        interestStore.fetchMyInterest(currentUser.uid).then((value) {
          if (value != null) {
            interests = ObservableList.of(value);
            trueDebugPrint("flutterting getmyinterest return $value");
          }
        }).catchError((e) {
          trueDebugPrint("flutterting getmyinterest error $e");
        });
      }
      if (myPosition == null) {
        _determinePosition().then((position){
          trueDebugPrint('获取地址');
          if(position != null){
            myPosition = position;
            var location = "${position.longitude},${position.latitude}";
            trueDebugPrint('获取地址' + location);
            UserApi.builder().reportLocation(location).then((value){
              trueDebugPrint('上报地址成功');
            });
          }
        }).catchError((error){
          trueDebugPrint('获取地址失败');
        });


      }
    }else{
      trueDebugPrint("flutterting setUserEntity null");
      myPosition = null;
      interests = ObservableList.of([]);
    }
  }



  @observable
  ObservableList<InterestBean> interests = ObservableList.of([]);

  @action
  addInterest(bean){
    interests.insert(0, bean);
  }

  @action
  removeInterest(bean){
    interests.remove(bean);
  }

  // @action
  // setInterest(List<InterestBean> beans){
  //   interests = beans;
  // }


  @observable
  int unReadUUMsgCount = 0;

  @observable
  int unReadUCGMsgCount = 0;

  @observable
  int unReadTodoMsgCount = 0;

  @action
  setUnReadTodoMsgCount(int i){
    unReadTodoMsgCount = i;
  }

  @action
  setUnReadUCGMsgCount(int i){
    unReadUCGMsgCount = i;
  }

  @action
  setUnReadUUMsgCount(int i){
    unReadUUMsgCount = i;
  }

  @action
  addUnReadTodoMsgCount(){
    unReadTodoMsgCount+= 1;
  }

  @action
  addUnReadUCGMsgCount(){
    unReadUCGMsgCount += 1;
  }

  @action
  addUnReadUUMsgCount(){
    unReadUUMsgCount += 1;
  }
}
